package com.loves.admin.service.impl;

import com.alibaba.excel.EasyExcel;
import com.loves.admin.model.User;
import com.loves.admin.service.ExcelService;
import com.loves.admin.service.UserService;
import com.loves.admin.vo.UserExportVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ExcelServiceImpl implements ExcelService {

    @Resource
    private UserService userService;

    @Override
    public void exportUser() {
        OutputStream outputStream = null;
        try {
            //不需要导出的字段
            Set<String> excludeColumnFiledNames = new HashSet<String>();
            excludeColumnFiledNames.add("id");
            excludeColumnFiledNames.add("deleteFlag");
            List<User> userList = userService.findAllList();
            String fileName = "用户信息表";
            outputStream = new FileOutputStream("D:/Downloads/exportExcel/"+fileName+System.currentTimeMillis()+".xlsx");
            EasyExcel.write(outputStream, User.class).excludeColumnFiledNames(excludeColumnFiledNames).sheet("第一页").doWrite(userList);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void dynamicExport() throws FileNotFoundException {
        String fileName = "动态用户信息表";
        OutputStream outputStream = new FileOutputStream("D:/Downloads/exportExcel/"+fileName+System.currentTimeMillis()+".xlsx");
        List<UserExportVO> userList = userService.findByExport();
        List<String> exportLine = userList.stream().map(UserExportVO::getExportName).distinct().collect(Collectors.toList());
        List<String> headName = new ArrayList<>();
        headName.add("用户姓名");
        headName.add("用户年龄");
        headName.add("用户性别");
        headName.add("创建时间");
        headName.add("创建人");
        headName.addAll(exportLine);
        // 所有行的集合
        List<List<String>> lineList = headName.stream().map(h -> {
            List<String> valueList = new ArrayList<>();
            valueList.add(h);
            return valueList;
        }).collect(Collectors.toList());
        //所有数据的集合
        List<List<Object>> valueList = new ArrayList<>();
        List<Object> valueLine = userList.stream().map(UserExportVO::getExportId).distinct().collect(Collectors.toList());
        userList.forEach(userExportVO -> {
            List<Object> values = new ArrayList<>();
            values.add(userExportVO.getName());
            values.add(userExportVO.getAge());
            values.add(userExportVO.getGender());
            values.add(userExportVO.getCreateDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            values.add(userExportVO.getCreateName());
            List<Object> countList = valueLine.stream().map(v -> {
                int count = 0;
                if (v.equals(userExportVO.getExportId())) {
                    count++;
                }
                return count;
            }).collect(Collectors.toList());
            values.addAll(countList);
            valueList.add(values);
        });

        EasyExcel.write(outputStream).head(lineList).sheet("第一页").doWrite(valueList);
    }

}
